package server;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Map;

public class LeaderMethod {
	
	public void updateNodeMaps(ServerImpl ser,Map<String, NodeRecord> records, NodeRecord node, int command){ // 0 pridani uzlu  1 odebrani uzlu
		Iterator<Map.Entry<String, NodeRecord>> entries = records.entrySet().iterator();
		while (entries.hasNext()) {
		    Map.Entry<String, NodeRecord> entry = (Map.Entry<String, NodeRecord>) entries.next();
		    try {
		    	if(command==1 && node.getName().equals(entry.getValue().getName())){} 
		    	else if (entry.getValue().getName().equals(ser.getMyname())){}
		    	else{entry.getValue().getInterface().updateMyMap(node,command);}
		   } catch (RemoteException e) {
			e.printStackTrace();
		   }
		}
	}
	
	public void sendLeaderDeath(ServerImpl ser,Map<String, NodeRecord> records){ 
		Iterator<Map.Entry<String, NodeRecord>> entries = records.entrySet().iterator();
		while (entries.hasNext()) {
		   Map.Entry<String, NodeRecord> entry = entries.next();
		   try {
			   if (!entry.getValue().getName().equals(ser.getMyname()))entry.getValue().getInterface().leaderIsDeath();
		   } catch (RemoteException e) {
			   e.printStackTrace();
		   }
		}
	}
}
